$(".shou").click(function(){
	$(this).css("color","#F9C486");
	$(".zhi").css("color","#FFFFFF");
	getList(1);//实物
})

$(".zhi").click(function(){
	$(this).css("color","#F9C486");
	$(".shou").css("color","#FFFFFF");
	getList(0)//门票
})

var user;
$(function(){
	getList(1)//门票

	$(".shou").css("color","#F9C486");
	$.ajax({
		url:'../userController/getUser',
		success:function(data){
			user = data;
			$(".bfb").html(data.mBalance.toFixed(2))
		}
	})
})

function getList(type) {
	$.ajax({
		data:{
			"type":type,
		},
		url:'../ticketController/payTickets',
		success:function(data){
			console.log(data);
			if(data[0]!=null && data[0]!=""){
				$(".payTicketBox1").html("");
				$.each(data[0],function(idex,item){
					$(".payTicketBox1").append("<div class='box'><div class='ticketPic' style='background:url("+item.ticketPic+") no-repeat;background-size:100% 100%;'></div><div class='Ticket'><div class='title'><div class='name'>"+item.ticket+"</div><div class='price'>￥"+item.price.toFixed(2)+"</div></div><div class='pay' onClick=\"bfbPay('"+item.id+"','"+item.type+"');\" >兑换</div></div></div>")
				})
			}
			if(data[1]!=null && data[1]!=""){
				$(".payTicketBox").html("");
				$.each(data[1],function(idex,item){
					$(".payTicketBox").append("<div class='box'><div class='ticketPic' style='background:url("+item.ticketPic+") no-repeat;background-size:100% 100%;'></div><div class='Ticket'><div class='title'><div class='name'>"+item.ticket+"</div><div class='price'>￥"+item.price.toFixed(2)+"</div></div><div class='pay' onClick=\"bfbPay('"+item.id+"','"+item.type+"');\" >兑换</div></div></div>")
				})
			}
		}
	});
}

var appId, timeStamp, nonceStr, packages, signType, paySign;
 function pay(price){
			var url = "../NotifyReqController/NotifyReq?price="+price
			$.get(url,function(result){
				appId = result.appId;
				timeStamp = result.timeStamp;
				nonceStr = result.nonceStr;
				packages = result.package;
				signType = result.signType;
				paySign = result.paySign;
				/* alert(packages);
				alert(signType); */
				if (typeof WeixinJSBridge == "undefined"){
		            if( document.addEventListener ){
		                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
		            }else if (document.attachEvent){
		                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
		                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
		            }
		        }else{
		            onBridgeReady();
		        }
			}) 

}

/**
 * 兑换商品
 *
 * @param price
 * @param type
 */
function bfbPay(id,type){
	if(type == 0){
		window.location.href = "../html/ProductDetail.html?id="+id;//地址页面
	}else{
		window.location.href = "../html/ProductDetailMenPiao.html?id="+id;//地址页面
	}

	/*var url = "../NotifyReqController/bfbPay?id="+id+"&type="+type
	$.get(url,function(result){

	})*/
}

function chongzhi() {
	var txt;
	var person = prompt("请输入充值金额(1元兑换10不凡币)：", "");
	if (person == null || person == "") {
		txt = "用户取消";
	} else {
		var isnum = /^\d+$/.test(person);
		if(!isnum){
			alert("请输入整数");
			return;
		}
		console.log(person)
		pay(person);
	}
}

function onBridgeReady() {
	WeixinJSBridge.invoke('getBrandWCPayRequest', {
		"appId" : appId,
		//公众号名称,由商户传入               
		"timeStamp" : timeStamp,
		//时间戳,自1970年以来的秒数               
		"nonceStr" : nonceStr,
		//随机串              
		"package" : packages,
		"signType" : signType,
		//微信签名方式：               
		"paySign" : paySign
	//微信签名          
	}, function(res) {
		if (res.err_msg == "get_brand_wcpay_request:ok") {
			console.log('支付成功');
			//支付成功后跳转的页面
			window.location.href = "myTicket.html";
		} else if (res.err_msg == "get_brand_wcpay_request:cancel"){
			//console.log('支付取消');
			window.location.href = "";
		} else if (res.err_msg == "get_brand_wcpay_request:fail") {
			//console.log('支付失败');
			WeixinJSBridge.call('closeWindow');
			window.location.href = "";
		}
		//使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
	});
}
